﻿<template>
  <div class="container">
      <div class='login_content'>
        <div class='user_photo'><open-data type="userAvatarUrl"></open-data></div>
        <div class='user_nickname'><open-data type="userNickName"></open-data></div>
        <button  v-if="canIUse" class="login_btn" open-type="getUserInfo" @getuserinfo="bindGetUserInfo">点击授权登录</button>
        <div v-else>请升级微信版本</div>
      </div>
  </div>
</template>
<script>
export default {
  data () {
    return {
      canIUse: wx.canIUse('button.open-type.getUserInfo'),
      loginType:''
    }
  },
  methods: {
    async bindGetUserInfo(e)
    {
      var _this = this
      wx.login
      ({
          success: res =>
          {
            let code = res.code;
            wx.getSetting({
          success(res) {
            if (res.authSetting['scope.userInfo']) {
              // 已经授权，可以直接调用 getUserInfo 获取头像昵称
              wx.getUserInfo({
                  success: res => {
                  // this.$store.dispatch('setUserInfo',res);   //把获取到的用户的信息存放在vuex中进行管理

                 _this.$request.postData('/auth/wxLogin',
                 {
                 raw_data: res.rawData,
                 signature: res.signature,
                 code: code,
                //  share_user_id:_this.friendId//游戏id
               }).then(res1 => {

                 if(res1.data.level == "success")
                 {

                   wx.setStorageSync('userToken', res1.data.data);

//          判断是否从gameHome跳转过来的
                let pageName = wx.getStorageSync('toPage').split("/")[2];
                if(pageName == "contentHome") {

                  //  存user_key
                  wx.setStorageSync("user_key", res1.data.user_key)
                  wx.reLaunch({
                    url: wx.getStorageSync('toPage')
                  })
                }

//           判断是否绑定手机
                if (res1.data.is_binding_phone == 0 && pageName!='gameHome') {
                  _this.$currency.navigateTo(`/pages/register/main?userToken=${res1.data.data}`)
                } else {
                  let toPageUrl = wx.getStorageSync('toPage')
                  if (toPageUrl != '') {
                    wx.navigateTo({
                      url: toPageUrl
                    })
                  } else {
                    _this.$currency.navigateTo("/pages/home/main")
                  }

                }

              }
            else
              {
                _this.$currency.showToast(res1.data.message, 'none', 2500)
              }
            })
            }
            })
            }
          },
          fail(res){
            console.log('err' + res)
          }
        })
          }
      })
    },
  },

  onLoad(options)
  {
    // if(options.friendId){
    //   this.friendId=options.friendId//游戏朋友的id
    // }
  },

  getPageName(path)
  {
//    return path.split("/")[1]
  }
}
</script>
<style scoped>
.login_content{width: 100%;position: absolute;left: 0;top: 20%;}
.user_photo{width: 200px;margin:0 auto;}
.user_nickname{text-align: center;margin-top: 20px}
button{margin-top: 30px;border-radius:2px;width: 68%;margin-left: 15%;border: solid 1px green;background-color: white;color: green}
</style>
